package com.corner.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.leancloud.helper.MessageHelperV2;
import com.corner.presenter.imp.SetNewPasswordPresenter;
import com.corner.ui.activity.LoginAndRegisterActivity;
import com.corner.ui.activity.SetPasswordActivity;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.base.BaseFragment;
import com.corner.ui.view.ISetNewPasswordView;
import com.corner.utils.AppManager;
import com.corner.utils.SoftInputUtil;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;

public class SetNewPasswordFragment extends BaseFragment<SetNewPasswordPresenter> implements
        OnClickListener, ISetNewPasswordView {
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_frag_forget_password_change_notice)
    private TextView tvNotice;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_sms_code_input)
    private EditText etCodeinput;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_password_input)
    private EditText etPasswordInput;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_set_new_password_finish)
    private TextView tvFinish;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.btn_register_login_actionbar_back)
    private Button btnBack;
    private String password;

    @Override
    protected SetNewPasswordPresenter createP() {
        return new SetNewPasswordPresenter();
    }

    @Override
    protected void setOnclick() {
        btnBack.setOnClickListener(this);
        tvFinish.setOnClickListener(this);
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.frag_forget_password_change, null);
    }

    @Override
    public void onClick(View v) {
        SoftInputUtil.hideSoftInput(activity, view);
        switch (v.getId()) {
            case R.id.tv_set_new_password_finish:
                finishChange();
                break;
            case R.id.btn_register_login_actionbar_back:
                if (activity instanceof SetPasswordActivity) {
                    activity.popBackStack(ForgetPasswordFragment.class.getSimpleName());
                    activity.changeFragment(
                            ((SetPasswordActivity) activity).forgetPasswordFragment,
                            this, false);
                } else if (activity instanceof LoginAndRegisterActivity) {
                    activity.popBackStack(ForgetPasswordFragment.class.getSimpleName());
                    activity.changeFragment(
                            ((LoginAndRegisterActivity) activity).getForgetPasswordFragment(),
                            this, false);
                }
                break;

        }
    }

    /**
     * 完成改变密码的操作
     */
    private void finishChange() {
        activity.excuteTaskWhenNetOk(new BaseActivity.Task() {

            @Override
            public void onExcute() {
                if (checkCode(etCodeinput.getText())) {
                    if (checkPassword(etPasswordInput.getText())) {
                        Bundle bundle = getArguments();
                        String phoneNum = bundle.getString(ForgetPasswordFragment.PHONE_NUM);
                        password = etPasswordInput.getText().toString();
                        presenter.setPasswordBySmsCode(phoneNum, etCodeinput.getText().toString(), etPasswordInput.getText().toString());
                    } else {
                        ToastUtil.showCustomToastInRelease(activity,
                                R.string.login_password_null_notice);
                    }
                } else {
                    ToastUtil.showCustomToastInDevelop(activity,
                            R.string.sms_code_error_notice);
                }
            }
        });

    }

    /**
     * 校验电话
     *
     * @param s
     * @return
     */
    private boolean checkCode(CharSequence s) {
        return s.length() == 6;
    }

    /**
     * 校验电话
     *
     * @param s
     * @return
     */
    private boolean checkPassword(CharSequence s) {
        return s.length() != 0;
    }

    @Override
    public void setPasswordSucess() {
        // 将前两个界面弹栈
        // activity.popBackStack();
        ToastUtil.showCustomToastInRelease(activity, activity.getString(R.string.set_password_sucess));
        if (activity instanceof LoginAndRegisterActivity) {
            activity.popBackStack(LoginFragment.class
                    .getSimpleName());
            activity.changeFragment(
                    ((LoginAndRegisterActivity) activity).getLoginFragment(),
                    SetNewPasswordFragment.this, false);
        } else if (activity instanceof SetPasswordActivity) {
            Bundle bundle = getArguments();
            String phoneNum = bundle.getString(ForgetPasswordFragment.PHONE_NUM);
            presenter.login(phoneNum, password);
        }
    }

    @Override
    public void setPasswordError(String message) {
        ToastUtil.showCustomToastInRelease(App.application, message);
    }

    @Override
    public void loginSucess(User user) {
        ToastUtil.showCustomToastInRelease(activity, R.string.login_sucess);
        AppManager.getInstance().finishActivity(activity);
    }

    @Override
    public void loginError(String errorMessage) {
        ToastUtil.showCustomToastInRelease(App.application, errorMessage);
        App.application.clearDaos();
        MessageHelperV2.closeImClient();
        User.logOut();
        AppManager.getInstance().finishAllActivity();
        Intent intent = new Intent(activity, LoginAndRegisterActivity.class);
        activity.startActivity(intent);
    }
}
